package com.tadpole.music.presenter.question;

import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.login.RegisterIView;
import com.tadpole.music.iView.question.SubmitErrorQuestionIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 错题上传
 * Created By 孙岩
 * on 2020/9/16
 */
public class SubmitErrorQuestionPresenter extends BasePresenter<SubmitErrorQuestionIView> {
    /**
     * @param college_id   学院id
     * @param rank_id      等级id
     * @param class_id     分类id
     * @param err_selected 错误选择的答案，中间用英文逗号隔开
     * @param err_qid      错题id，中间用逗号隔开
     * @param time         答题时间
     */
    @SuppressWarnings("unchecked")
    public void setErrorQuestion(final int type ,String college_id, String rank_id, String class_id, String err_selected, String err_qid, String time) {
        if (isViewAttached()) {
            String url = UrlConfig.errQuestion;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.POST_HEAD_REQUEST)
                    .token(token)
                    .url(url)
                    .paramKey("college_id", "rank_id", "class_id", "err_selected", "err_qid", "time")
                    .paramValue(college_id, rank_id, class_id, err_selected, err_qid, time)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    getView().showResult(type);
                                } else if(obj.getInt("code")==401){
                                    getView().show401();
                                }else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {
                            getView().hideLoading();
                        }

                        @Override
                        public void onBefore() {
                            getView().showLoading();
                        }
                    });
        }
    }
}
